<script setup>
import { useUserStore } from "./store/user.js";
const userStore = useUserStore();

const signInBtn = document.getElementById("signIn");
const signUpBtn = document.getElementById("signUp");
const fistForm = document.getElementById("form1");
const secondForm = document.getElementById("form2");
const container = document.querySelector(".container");
const btn1 = document.querySelector("#btn1");
const btn2 = document.querySelector("#btn2");
const signUpName = document.querySelector("#signUpName");
const signUpUserID = document.querySelector("#signUpUserID");
const signUpPassword = document.querySelector("#signUpPassword");
const signInUserId = document.querySelector("#signInUserId");
const signInPassword = document.querySelector("#signInPassword");
const identity = document.querySelector("select");

signInBtn.addEventListener("click", () => {
  container.classList.remove("right-panel-active");
});

signUpBtn.addEventListener("click", () => {
  container.classList.add("right-panel-active");
});

fistForm.addEventListener("submit", (e) => e.preventDefault());
secondForm.addEventListener("submit", (e) => e.preventDefault());
btn1.addEventListener("click", () => {});
btn2.addEventListener("click", () => {
  if (signInUserId.value === "" || signInPassword.value === "") {
    alert("请输入账号密码！");
  } else {
    let userInfo = {
      uid: signInUserId.value,
      pwd: signInPassword.value,
      identity: identity.value,
    };
    // console.log(userInfo);

    fetch("http://localhost:9090/checkUser", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(userInfo),
    })
      .then((response) => {
        response.json().then((data) => {
          // console.log("账户检验：", data);
          if (data === false) {
            alert("账号或密码错误！");
          } else {
            // 从服务器获取用用户数据
            fetch("http://localhost:9090/getUserInfo", {
              method: "POST",
              headers: {
                "Content-Type": "application/json",
              },
              body: JSON.stringify(userInfo),
            })
              .then((response) => {
                response.json().then((data) => {
                  // 用户信息
                  console.log("用户信息", data);
                  userStore.setUid(data.uid);
                  userStore.setPwd(data.pwd);
                  userStore.setIdentity(data.identity);
                  userStore.setUname(data.uname);

                  if (userStore.identity === "教师") {
                    window.location.href = "/src/pages/teacherHome/home.html";
                  } else {
                    window.location.href = "/src/pages/studentHome/home.html";
                  }
                });
              })
              .catch((err) => {
                console.log(err);
              });
          }
        });
      })
      .catch((err) => {
        console.log(err);
      });
  }
});

userStore.storeInit();
</script>

<template></template>